package com.zgz.underwrite.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.zgz.underwrite.dao.ProductDao;
import com.zgz.underwrite.model.Product;

public class ProductAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public final static Logger log = Logger.getLogger(ProductAction.class);
	private ProductDao productDao;
	private Product product;
	
	public String newProduct(){
		log.info(">>>>>>>>>>>>>>>>>新增产品......");
		//--------test start--------------
//		Product prod = new Product();
//		prod.setSProductCde("46456464564654");
//		prod.setSProductName("hahhah");
		//--------test end--------------
		try {
			productDao.save(this.product);
			log.info(">>>>>>>>>>>>>>>>>新增产品成功......");
			return SUCCESS;
		} catch (Exception e) {
			log.info(">>>>>>>>>>>>>>>>>新增产品出异常.....");
			e.printStackTrace();
		}
		return INPUT;
	}
	
	public String deleteProduct(){
		log.info(">>>>>>>>>>>>>>>>>删除产品.....");
		try {
			this.productDao.delete(this.product);
			log.info(">>>>>>>>>>>>>>>>>删除产品成功......");
			return SUCCESS;
		} catch (Exception e) {
			log.info(">>>>>>>>>>>>>>>>>删除产品不成功......");
			e.printStackTrace();
		}
		return ERROR;
	}
	
	
	public String findAllProducts(){
		log.info(">>>>>>>>>>>>>>>>>查询产品.....");
		List<Product> products = null;
		try {
			products = (List<Product>) this.productDao.getAll();
			super.request.setAttribute("products", products);
			log.info(">>>>>>>>>>>>>>>>>查询产品成功.....");
			return SUCCESS;
		} catch (Exception e) {
			log.info(">>>>>>>>>>>>>>>>>查询产品出异常.....");	
			e.printStackTrace();
		}
		return ERROR;
	}
	

	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product getProduct() {
		return product;
	}
}
